﻿using BySoft.Server.Business;
using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class QuerySOutProcess : BasePage
    {
        int m_count = 0;

         
        //protected DataTable BillDT
        //{
        //    get
        //    {
        //        if (Session["BillDT"] != null)
        //        {
        //            return (DataTable)Session["BillDT"];
        //        }
        //        else
        //        {
        //            return CreateDT();
        //        }
        //    }
        //    set
        //    {
        //        Session["BillDT"] = value;
        //    }
        //}


        private DataTable CreateDT()
        {
            return BsOutorder.CreateDT();
        }
       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData(0);
            }
        }
        private void BindData(int pageIndex)
        {

            DataTable dt = BsOutorder.SelectListProcessID(txtProcessID.Text, txtWorkOrderNo.Text.Trim(), pageIndex, pcData.PageSize, out m_count);
            pcData.RecordCount = m_count;
            if (dt.Rows.Count != 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
                pcData.CurrentIndex = pageIndex;

            } else{
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData(0);
        }

        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                RadioButton rb = (RadioButton)e.Row.FindControl("rbSelect");
                rb.Attributes.Add("onclick", "judge(this)");
            }
        }


        private void AddPro()
        {
            DataTable _BillDT = CreateDT();
              GetAddPro(_BillDT).Copy();
        }

        string m_strResult = "";
        private DataTable GetAddPro(DataTable dt)
        {
            DataTable dtTemp = dt.Copy();
            foreach (GridViewRow gvr in grdResult.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    HiddenField lblDetail = gvr.FindControl("hdScheduleDetailID") as HiddenField;

                    if (((RadioButton)gvr.FindControl("rbSelect")).Checked)
                    {

                        Label lblID = gvr.FindControl("lblProcessID") as Label;
                        Label lblProcessName = gvr.FindControl("lblProcessName") as Label;
                        Label lblWorkOrderNo = gvr.FindControl("lblWorkOrderNo") as Label;
                        Label lblScheduleID = gvr.FindControl("lblScheduleID") as Label;
                        Label lblPlanID = gvr.FindControl("lblPlanID") as Label;
                        Label lblExpectInStockDate = gvr.FindControl("lblExpectInStockDate") as Label;
                        Label lblScheduleNum = gvr.FindControl("lblScheduleNum") as Label;

                        string strScheduleDetailID = lblDetail.Value.Trim();
                        string strProcessID = lblID.Text.Trim();
                       
                        //MainDT
                        DataTable mainDt = CreateDT();

                        DataRow drM = mainDt.Rows.Add();
                        
                        drM["ProcessID"] = strProcessID;
                        drM["ScheduleDetailID"] = strScheduleDetailID;

                        drM["WorkOrderNo"] = lblWorkOrderNo.Text;
                        drM["ProcessName"] = lblProcessName.Text;
                        drM["ScheduleID"] = lblScheduleID.Text;
                        drM["PlanID"] = lblPlanID.Text;
                        drM["ExpectInStockDate"] = lblExpectInStockDate.Text;

                        drM["ScheduleNum"] = lblScheduleNum.Text;
                        drM["IsNew"] = "1";
                        drM["IsDel"] = "-1";

                        dtTemp = mainDt.Copy();

                        m_strResult = string.Format(@"{0},{1},{2},{3},{4},{5}", 
                            strProcessID, strScheduleDetailID,lblScheduleID.Text,
                            lblWorkOrderNo.Text, lblProcessName.Text,lblScheduleNum.Text);

                    }
                    else
                    {
                        DataRow[] drs = dtTemp.Select("ScheduleDetailID='" + lblDetail.Value.Trim() + "'  ");
                        if (drs.Length > 0)
                        {
                            drs[0]["IsDel"] = "1";
                        }
                    }
                }
            }
            return dtTemp;
        }
        protected void btnOk_Click(object sender, EventArgs e)
        {
            AddPro();
            
             
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'" + m_strResult + "');CloseTopDialog();</script>");
        }

        //protected void btnOk_Click(object sender, EventArgs e)
        //{
        // //   DataTable dtProcessDetail = BsOutorder.SelectListNeedOutProcess("", txtProcessName.Text, pageIndex, pcData.PageSize, out m_count);

        //    string sReturn = "";
        //    foreach (GridViewRow gvr in grdResult.Rows)
        //    {



        //        if (gvr.RowType == DataControlRowType.DataRow)
        //        {
        //            if (((RadioButton)gvr.FindControl("rbSelect")).Checked)
        //            {
        //                Label lblID = gvr.FindControl("lblProcessID") as Label;
        //                Label lblName = gvr.FindControl("lblProcessName") as Label;
        //                HiddenField lblDetail = gvr.FindControl("hdScheduleDetailID") as HiddenField;

        //                string strResult = lblID.Text.Trim() + "," +
        //                  lblName.Text.Trim();
        //                sReturn = HttpUtility.UrlEncode(strResult, Encoding.UTF8);
        //                break;
        //            }
        //        }

        //    }

        //    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'" + sReturn + "');CloseTopDialog();</script>");
        //}

        protected void pcData_PageIndexClick(object sender, Controls.PageIndexClickEventArgs e)
        {
            BindData(e.PageIndex);
        }
    }
}